// ********************************
// Plugin Slide Image - GoSlideImagePlugIn
// Version : 0.01
// Created by : tuanhnm
// Created date : 09/05/2011
// ********************************

// **********
// Begin create plugin
// **********
(function(){

$.fn.GoSlideImageBasic = function () {
      
	  // Define repeat function to join array
	  function repeat(str, n) {
            return new Array( n + 1 ).join(str);
      }
	  
	  return this.each(function(){
	  
		// Get direct child div of parent div and set this css to overflow hidden (check if browsers enable javascript) 
            var $wrapper = $('> div', this).css('overflow', 'hidden'),
               
			   // Get ul items of wrapper div and set width 9999
				$slider = $wrapper.find('> .slider').width(9999),
				
				// Get li item of ul element 
                $items = $slider.find('> .slide_item'),
				
				// Get single item in li list
                $single = $items.filter(':first')
                
				// Get width of single item comprise = width + padding in li list
                singleWidth = $single.outerWidth(),
				
				// Calculate nums item can be visible by formal : innerWidth's wrapper div / outerWidth's first li item in li list = 315 / 105 = 3 items visible/ page
                visible = Math.ceil($wrapper.innerWidth() / singleWidth),
                
				// Set default position page at 1
				currentPage = 1,
				
				// Calculate total page by formal : total item of li list / num visible item or page : 15 / 3 = 5 pages
                pages = Math.ceil($items.length / visible);
                
            /* TASKS */
            
            // 1. pad the pages with empty element if required
            if ($items.length % visible != 0) {
                // pad
                $slider.append(repeat('<div class="empty" />', visible - ($items.length % visible)));
                $items = $slider.find('> .slide_item');
            }
            
            // 2. create the carousel padding on left and right (cloned)
			// Clone last li of items to insert into start of the list
            $items.filter(':first').before($items.slice(-visible).clone().addClass('cloned'));
			// Clone first li of items to insert into end of the list
            $items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
            $items = $slider.find('> .slide_item');
            
            // 3. reset scroll
            $wrapper.scrollLeft(singleWidth * visible);
            
            // 4. paging function
            function gotoPage(page) {
                var dir = page < currentPage ? -1 : 1,
                    n = Math.abs(currentPage - page),
                    left = singleWidth * dir * visible * n;
                
                $wrapper.filter(':not(:animated)').animate({
                    scrollLeft : '+=' + left
                }, 500, function () {
                    // if page == last page - then reset position
                    if (page > pages) {
                        $wrapper.scrollLeft(singleWidth * visible);
                        page = 1;
                    } else if (page == 0) {
                        page = pages;
                        $wrapper.scrollLeft(singleWidth * visible * pages);
                    }
                    
                    currentPage = page;
                });
            }
            
            // 5. insert the back and forward link
            //$wrapper.after('<a href="#" class="arrow back">&lt;</a><a href="#" class="arrow forward">&gt;</a>');
            
			//$wrapper.before('<div id="product_slide_right" class="product_slide_right"><img src="Images/bt_next.png" width="21" height="24" align="absmiddle" />&nbsp;</div>');
			
			//$wrapper.after('<div id="product_slide_right" class="product_slide_right"><img src="Images/bt_next.png" width="21" height="24" align="absmiddle" />&nbsp;</div>');
			
            // 6. bind the back and forward links
            $('a.back', this).click(function () {
                gotoPage(currentPage - 1);
                return false;
            });
            
            $('a.forward', this).click(function () {
                gotoPage(currentPage + 1);
                return false;
            });
            
            $(this).bind('goto', function (event, page) {
                gotoPage(page);
				return false;
            });
            
            // THIS IS NEW CODE FOR THE AUTOMATIC INFINITE CAROUSEL
            $(this).bind('next', function () {
                gotoPage(currentPage + 1);
				return false;
            });
			
			$(this).bind('prev', function(){
				gotoPage(currentPage - 1);
				return false;
			});
		
	  });
};

})(jQuery);
// **********
// End create plugin
// **********

